package com.cet.programmercarl.algorithmperformancanalysis.哈希表;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: algorithm
 * @description: 快乐数
 * @author: 陈恩涛
 * @create: 2022-03-15 10:57
 **/
public class LC202 {
    public static boolean isHappy(int n) {
        Set<Integer> sum = new HashSet<>();
        while (!sum.contains(n)) {
            sum.add(n);
            if ((n = getSum(n)) == 1) {
                return true;
            }
        }
        return false;
    }

    public static int getSum(int n) {
        int sum = 0;
        while (n != 0) {
            sum += (n % 10) * (n % 10);
            n = n / 10;
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(isHappy(7));
    }
}
